package cn.yj.core.business.controller;

import cn.yj.cgs.dba.entity.Message;
import cn.yj.admin.frame.AbstractController;
import cn.yj.syslog.common.LogType;
import cn.yj.syslog.common.OperateLog;
import cn.yj.core.business.service.MessageService;
import cn.yj.entity.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.Map;

/**
 * @author 永健
 * @since 2021-06-16 11:42
 */
@RestController
@RequestMapping("/message")
public class MessageController extends AbstractController<Message> {
    @Resource
    private MessageService messageService;

    @Override
    @GetMapping("/pageList")
    @RequiresPermissions("message:list")
    public R pageList(@RequestParam Map<String, Object> params) {
        return success(messageService.pageList(params, page()));
    }

    /**
     * 群发权限
     * @param message
     * @return
     */
    @PostMapping("/save")
    @RequiresPermissions("message:add")
    @OperateLog(describe = "群发消息",type = LogType.INSERT)
    public R save(@RequestBody @Valid Message message) {
        return success(messageService.save(message));
    }

    @PostMapping("/saveOne")
    @RequiresPermissions("message:add:one")
    @OperateLog(describe = "给用户发消息",type = LogType.INSERT)
    public R saveOne(@RequestBody @Valid Message message) {
        return success(messageService.save(message));
    }

    @DeleteMapping("/delete/{ids}")
    @RequiresPermissions("message:del")
    @OperateLog(describe = "消息删除",type = LogType.DELETE)
    public R read(@PathVariable Integer[] ids) {
        return success(messageService.removeByIds(Arrays.asList(ids)));
    }
}
